import { bulletManager } from "./BulletManager";
import MyLog, { MYLOG_TYPE } from "./utils/MyLog";

const { ccclass, property } = cc._decorator;


export class BulletInfo {
    /**子弹飞行速度 */
    speed: number = null || 1200;
    type: string = null;
    position: cc.Vec3 = null;
    angle: number = null || 0;
    id: number = null;

    /**
     * 
     * @param position 位置
     * @param speed 速度
     * @param angle 角度
     * @param type 类型
     * @param id 
     */
    constructor(position, speed, angle, type, id) {
        this.type = type
        this.id = id
        this.speed = speed
        this.angle = angle
        this.position = position
    }
}

@ccclass
export default class BulletCtr extends cc.Component {

    @property(cc.SpriteAtlas)
    bulletAtlas: cc.SpriteAtlas = null;

    @property(cc.Sprite)
    bullet_type: cc.Sprite = null;

    bulletImg: cc.Sprite = null;

    /**子弹配置信息 */
    bulletInfo: BulletInfo = null;

    /**x 轴飞行速度 */
    private speedX: number = null;

    /**Y 轴飞行速度 */
    private speedY: number = null;

    // onLoad () {}

    start() {

    }

    /**
     * 子弹回收时 对子弹进行恢复初始化
     */
    unuse() {
        
    }

    /**
     * 子弹取出来时 暂不操作
     */
    reuse() {
     
    }

    /**
     * 初始化子弹
     * @param type 子弹类型 
     * @param position 子弹位置 
     * @param angle 子弹角度 
     */
    initBullet(bulletInfo: BulletInfo) {
        this.bulletImg = this.bullet_type.getComponent(cc.Sprite)
        this.bulletImg.spriteFrame = bulletManager.getBulletAtlas().getSpriteFrame(bulletInfo.type)
        this.node.position = bulletInfo.position
        this.node.angle = bulletInfo.angle
        this.bulletInfo = bulletInfo

        //角度转弧度
        var radians = cc.misc.degreesToRadians(bulletInfo.angle);

        //算出x、y方向的偏移量
        var point = new cc.Vec2(Math.cos(radians), Math.sin(radians))
        this.speedX = bulletInfo.speed * point.y
        this.speedY = bulletInfo.speed * point.x
    }

    update(dt) {
        this.node.x += this.speedX * dt;
        this.node.y += this.speedY * dt;

        //调整子弹自身的角度
        if (this.speedX > 0 && this.speedY > 0) {
            this.node.angle = -Math.atan(this.speedX / this.speedY) * 180 / Math.PI
        } else if (this.speedX > 0 && this.speedY < 0) {
            this.node.angle = -Math.atan(-this.speedY / this.speedX) * 180 / Math.PI - 90
        } else if (this.speedX < 0 && this.speedY > 0) {
            this.node.angle = Math.atan(-this.speedX / this.speedY) * 180 / Math.PI
        } else if (this.speedX < 0 && this.speedY < 0) {
            this.node.angle = -Math.atan(this.speedX / this.speedY) * 180 / Math.PI + 180
        }

        //子弹触碰到边缘时 发生发射
        if (this.node.x >= cc.winSize.width / 2 || this.node.x <= -cc.winSize.width / 2) {
            this.speedX *= -1
        }
        if (this.node.y >= cc.winSize.height / 2 || this.node.y <= -cc.winSize.height / 2) {
            this.speedY *= -1
        }

        //飞出屏幕之后回收子弹
        // this.node.x > cc.winSize.width/2 ?   this.recycleBullet() : null
        // this.node.x < -cc.winSize.width/2 ?  this.recycleBullet() : null
        // this.node.y > cc.winSize.height/2 ?  this.recycleBullet() : null
        // this.node.y < -cc.winSize.height/2 ? this.recycleBullet() : null
    }

    recycleBullet() {
        bulletManager.putBullet(this.node)
    }
}
